/**
 * @file VenuesService.ts
 * @description Venue service for managing venue data
 * @date 2024-6-4
 * @version 1.0.0
 */
import ActivityModel, {IActivity} from "../models/ActivityModel";

class ActivityService {
    /** 创建活动 */
    public async createActivity(params: IActivity) {
        try {
            const activity = await ActivityModel.create(params);
            return activity;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

    /** 查询活动列表 */
    public async getActivityList(params: {
        pageSize: number;
        pageIndex: number;
        activityName: string;
        startTime: number;
    }) {
        try {
            const {pageSize, pageIndex, activityName, startTime} = params;
            const filterRules: any = {};
            if (activityName) {
                filterRules.activityName = {$regex: new RegExp(activityName)};
            }
            if (startTime) {
                filterRules.startTime = {$gte: startTime};
            }
            const total = await ActivityModel.find().countDocuments();
            const list = await ActivityModel.find()
                .where(filterRules)
                .skip((pageIndex - 1) * pageSize)
                .limit(pageSize)
                .select([
                    "userId",
                    "activityName",
                    "startTime",
                    "endTime",
                    "venueId",
                    "createdAt",
                    "updatedAt",
                ])
                .exec();
            return {
                total,
                pageIndex,
                pageSize,
                list,
            };
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

    /** 更新活动信息 */
    public async updateActivity(params: IActivity) {
        try {
            const activity = await ActivityModel.findOneAndUpdate(
                {activityId: params.activityId},
                params,
                {
                    new: true,
                }
            );
            return activity;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

    /** 删除活动 */
    public async deleteActivity(activityId: string) {
        try {
            const activity = await ActivityModel.findOneAndDelete({activityId});
            return activity;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }

    /** 获取活动信息 */
    public async getActivityById(activityId: string) {
        try {
            const activity = await ActivityModel.findOne({activityId}).select([
                "userId",
                "activityName",
                "startTime",
                "endTime",
                "venueId",
                "createdAt",
                "updatedAt",
            ]);
            return activity;
        } catch (error) {
            console.error(error);
            throw error;
        }
    }
}

export default new ActivityService();
